<?php

/**
 * validacionVisitante actions.
 *
 * @package    ehockey
 * @subpackage validacionVisitante
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class validacionVisitanteActions extends sfActions
{
	public function executeIndex(sfWebRequest $request)
	{
		$this->notificacions = Doctrine::getTable('Notificacion')
		->createQuery('a')
		->execute();
	}

	public function executeShow(sfWebRequest $request)
	{
		$this->notificacion = Doctrine::getTable('Notificacion')->find(array($request->getParameter('id')));
		$this->forward404Unless($this->notificacion);
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->partido = $this->getRoute()->getObject();
		$this->form = new NotificacionForm();
		if ($this->partido)
		{
			$this->form->setPartidoId($this->partido->getId());
		}
		else
		{
			$this->redirect('@homepage');
		}
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));
		//$this->partido = $this->getRoute()->getObject();
		$this->form = new NotificacionForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($notificacion = Doctrine::getTable('Notificacion')->find(array($request->getParameter('id'))), sprintf('Object notificacion does not exist (%s).', $request->getParameter('id')));
		$this->form = new NotificacionForm($notificacion);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($notificacion = Doctrine::getTable('Notificacion')->find(array($request->getParameter('id'))), sprintf('Object notificacion does not exist (%s).', $request->getParameter('id')));
		$this->form = new NotificacionForm($notificacion);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$this->forward404Unless($notificacion = Doctrine::getTable('Notificacion')->find(array($request->getParameter('id'))), sprintf('Object notificacion does not exist (%s).', $request->getParameter('id')));
		$notificacion->delete();

		$this->redirect('validacionVisitante/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		
		if ($form->isValid())
		{
			$notificacion = $form->save();

			$notif = $form->getNotificacion();
			$partido = $notif->getPartido();
			if($form->isAceptada()) {
				LogPlanilla::logValidacionPlanillaAceptada($this->getUser()->getUsuario(), $partido);
			}else{
				LogPlanilla::logValidacionPlanillaConObservacion($this->getUser()->getUsuario(), $partido, $form->getNotificacion()->getMensaje());
			}
			
			$this->redirect('@homepage');
		}

	}
}
